package cn.endeavour.quartz.job;


import cn.endeavour.order.domain.ProductOrder;
import cn.endeavour.order.service.IAdoptOrderService;
import cn.endeavour.order.service.IProductOrderService;
import cn.endeavour.pay.constants.PayConstants;
import cn.endeavour.pay.domain.PayBill;
import cn.endeavour.pay.mapper.PayBillMapper;
import cn.endeavour.quartz.constants.JobConstants;
import cn.endeavour.quartz.domain.QuartzJobInfo;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Component
public class MainJob implements Job {

    @Autowired
    private IAdoptOrderService adoptOrderService;

    @Autowired
    private IProductOrderService productOrderService;

    /**
     * 定时器任务核心代码
     *
     * @param jobExecutionContext
     * @throws JobExecutionException
     */
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("定时器开始");

        JobDataMap jobDataMap = jobExecutionContext.getMergedJobDataMap();
        // 获取参数
        QuartzJobInfo jobInfo = (QuartzJobInfo) jobDataMap.get("params");
        // 订单号
        Map<String, Object> map = jobInfo.getParams();
        System.out.println(jobInfo.getCron());
        // 查询订单是否支付
        // 如果订单状态还是处于未支付状态,则设置为取消
        // 更新订单
        switch (jobInfo.getType()) {
            case JobConstants
                    .OVER_TIME_NO_CONFIRM_ADOPT:
                break;
            case JobConstants
                    .OVER_TIME_NO_CONFIRM_GOODS:
                break;
            case JobConstants
                    .OVER_TIME_NO_CONFIRM_PRODUCT:
                break;
            case JobConstants
                    .OVER_TIME_NO_CONFIRM_RECHARGE:
                break;
            case JobConstants
                    .OVER_TIME_NO_PAY_ADOPT: {
                Long orderId = (Long) map.get("orderId");
                if (orderId != null) {
                    adoptOrderService.cancel(orderId);
                }
                break;
            }
            case JobConstants
                    .OVER_TIME_NO_PAY_GOODS:
                break;
            case JobConstants
                    .OVER_TIME_NO_PAY_PRODUCT: {
                Long productId = (Long) map.get("productId");
                if (productId != null) {
                    productOrderService.cancelOrder(productId);
                }
                break;
            }
            case JobConstants
                    .OVER_TIME_NO_PAY_RECHARGE:
                break;

        }


    }
}
